package com.wenx.juc;

import java.util.concurrent.SynchronousQueue;
import java.util.stream.IntStream;

/**
 * 数据同步交换的队列
 */
public class MySynchronrousQueue {

    public static void main(String[] args) {

        SynchronousQueue<Integer> integers = new SynchronousQueue<>();

        //并行插入插入数据
        new Thread(() -> {
            IntStream.range(0,10)
                    .forEach(i -> {
                        System.out.println("放入: "+ i);
                        try {
                            integers.put(i);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    });
        }).start();
        //读取数据
        new Thread(() -> {
            while (true) {
                try {
                    System.out.println("取出: " + integers.take());
                    Thread.sleep((long)Math.random()*2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

}
